package com.xuanwei.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.annos.Positive;
import com.xuanwei.core.entity.param.Comment;
import com.xuanwei.core.entity.vo.*;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwOrderCommentService;
import com.xuanwei.core.service.IXwOrderService;
import com.xuanwei.dao.entity.XwOrder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/11 16:10
 **/
@RestController
@RequestMapping("wx/order")
@Validated
public class OrderController extends BasicController {

    private IXwOrderService orderService;


    private IXwOrderCommentService orderCommentService;


    public OrderController(IXwOrderService orderService,
                           IXwOrderCommentService orderCommentService) {
        this.orderService = orderService;
        this.orderCommentService = orderCommentService;
    }

    /**
     * 购物车跳转订单页面所需数据
     *
     * @param cartId 购物车ID
     * @return 响应
     */
    @RequestMapping(value = "cartToOrder", method = RequestMethod.GET)
    public ResultVO cartToOrder(@NotBlank(message = "购物车ID不能为空") String cartId, Integer couponId, Integer addressId) {

        List<Integer> ids = Arrays.stream(cartId.split("-"))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        return ResultVO.successResult(orderService.getOrderData(ids, getUserId(), couponId, addressId));
    }

    /**
     * 直接跳转订单页面
     *
     * @param skuId 规格ID
     * @param count 数量
     * @return 响应
     */
    @RequestMapping(value = "toOrder", method = RequestMethod.GET)
    public ResultVO toOrder(@Min(value = 1, message = "规格id格式错误") Integer skuId,
                            @Min(value = 1, message = "数量格式错误") Integer count, Integer couponId, Integer addressId) {

        PayOrderVo orderData = orderService.getOrderData(skuId, count, getUserId(), couponId, addressId);

        return ResultVO.successResult(orderData);
    }

    /**
     * 查询用户订单列表
     *
     * @param type 类型
     * @return 响应
     */
    @RequestMapping(value = "userOrderList", method = RequestMethod.GET)
    public ResultVO userOrderList(Integer type) {

        Page<XwOrder> page = getPage();
        List<OrderVo> userOrder = orderService.findUserOrder(getUserId(), type, page);
        return ResultVO.successResult(userOrder);
    }

    /**
     * 订单详情
     *
     * @param orderId 订单ID
     * @return 响应
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public ResultVO detail(@Min(value = 1, message = "订单Id参数错误") Integer orderId) {

        OrderDetailVo detail = orderService.detail(orderId);
        return ResultVO.successResult(detail);
    }

    /**
     * 关闭订单
     *
     * @param orderId 订单ID
     * @return 数据
     */
    @RequestMapping(value = "close", method = RequestMethod.GET)
    public ResultVO close(@Min(value = 1, message = "订单Id参数错误") Integer orderId,
                          @NotBlank(message = "原因不能为空") String reason) {

        orderService.orderClose(orderId, reason);
        return ResultVO.successResult("关闭成功");
    }

    /**
     * 删除订单
     *
     * @param orderId 订单ID
     * @return 数据
     */
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public ResultVO delete(@Min(value = 1, message = "订单Id参数错误") Integer orderId) {

        orderService.orderDelete(orderId);
        return ResultVO.successResult("删除成功");
    }

    /**
     * 获取物流信息
     *
     * @param orderId 订单ID
     * @return 数据
     */
    @RequestMapping(value = "courierInfo", method = RequestMethod.GET)
    public ResultVO courierInfo(@Min(value = 1, message = "订单ID格式错误") Integer orderId) {

        CourierInfoVo courierInfo = orderService.findCourierInfo(orderId);

        return ResultVO.successResult(courierInfo);
    }


    @RequestMapping(value = "receive", method = RequestMethod.GET)
    public ResultVO receive(@Positive(message = "订单ID格式错误") Integer orderId) {
        orderService.receive(orderId, getUserId());

        return ResultVO.successResult("操作成功");
    }

    /**
     * 获取各种订单数量
     *
     * @return 响应
     */
    @RequestMapping(value = "orderCount", method = RequestMethod.GET)
    public ResultVO orderCount() {
        OrderCountVo orderCount = orderService.findOrderCount(getUserId());

        return ResultVO.successResult(orderCount);
    }


    @RequestMapping(value = "getComment", method = RequestMethod.GET)
    public ResultVO getComment(@Positive(message = "ID参数错误") Integer id) {
        OrderCommentVo orderComment = orderService.findOrderComment(id);
        return ResultVO.successResult(orderComment);
    }

    /**
     * 评论
     *
     * @param comment 参数
     * @return 响应
     */
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    public ResultVO comment(@RequestBody Comment comment) {
        orderService.comment(comment.getCommentItems(), comment.getOrderId(), getUserId());
        return ResultVO.successResult("评论成功");
    }

    /**
     * 查询评论
     *
     * @param orderId 参数
     * @return 数据
     */
    @RequestMapping(value = "queryComment", method = RequestMethod.GET)
    public ResultVO queryComment(@Positive(message = "ID参数错误") Integer orderId) {

        OrderCommentVo detail = orderCommentService.findDetail(orderId);
        return ResultVO.successResult(detail);
    }


    /**
     * 申请退款
     *
     * @param orderId 订单ID
     * @param reason  原因
     * @return 响应
     */
    @RequestMapping(value = "applyRefund", method = RequestMethod.GET)
    public ResultVO applyRefund(@Positive(message = "订单ID不能为空") Integer orderId,
                                @NotBlank(message = "原因不能为空") String reason) {
        orderService.applyRefund(orderId, reason);

        return ResultVO.successResult("申请成功");
    }

    /**
     * 用户售后订单
     *
     * @return 响应
     */
    @RequestMapping("refundOrderList")
    public ResultVO refundOrderList() {
        List<OrderVo> userRefund = orderService.findUserRefund(getUserId(), getPage());

        return ResultVO.successResult(userRefund);
    }

    /**
     * 退款详情
     *
     * @param orderId 订单编号
     * @return 响应
     */
    @RequestMapping(value = "refundDetail", method = RequestMethod.GET)
    public ResultVO refundDetail(@Positive(message = "订单ID不能为空") Integer orderId) {
        RefundDetail refundDetail = orderService.findRefundDetail(orderId);
        return ResultVO.successResult(refundDetail);
    }

    /**
     * 继续订单
     *
     * @param orderId 订单编号
     * @return 响应
     */
    @RequestMapping(value = "continueOrder", method = RequestMethod.GET)
    public ResultVO continueOrder(@Positive(message = "订单ID不能为空") Integer orderId) {
        orderService.goOnOrder(orderId);
        return ResultVO.successResult();
    }

    /**
     * 继续退款
     *
     * @param orderId 订单编号
     * @return 响应
     */
    @RequestMapping(value = "continueRefund", method = RequestMethod.GET)
    public ResultVO continueRefund(@Positive(message = "订单ID不能为空") Integer orderId) {
        orderService.continueRefund(orderId);
        return ResultVO.successResult();
    }
}
